<?php

require_once("../../connection.php");

require_once("../../article_category/to/article_category_to.php");
require_once("../../article_category/dao/article_category_dao.php");


class Mysql_article_category_dao implements Article_category_dao {

	public function create($article_category_to) {
		try {
			$link = Connection::singleton()->transaction_begin();
	
			$title = mysql_real_escape_string($article_category_to->get_title());
			$description = mysql_real_escape_string($article_category_to->get_description());
			$autor_login = mysql_real_escape_string($article_category_to->get_autor_login());
			$ranking = $article_category_to->get_ranking();
			
			$result = mysql_query("insert into article_category	
									(title, description, autor_login, ranking) values 
									('$title', '$description', '$autor_login', '$ranking')",$link);
									
			if (!$result || (mysql_affected_rows($link) != 1) || 
					(($number = mysql_insert_id($link)) == 0)) 
					throw new Exception(NOT_ROW_INSERTED);
			
			Connection::singleton()->transaction_commit($link);
			
			$article_category_to = new Article_category_to($number, $title, $description, 
															$autor_login, $ranking);
			return $article_category_to;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function exist($number){
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_category
									where number='$number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1)) {
				Connection::singleton()->transaction_commit($link);
				return false;								
			}

			Connection::singleton()->transaction_commit($link);
			return true;
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function fin_by_number_article_category($number) {
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$result = mysql_query("select * from article_category
									where number='$number'", $link);
									
			if (!$result || (mysql_num_rows($result) != 1))
				throw new Exception(NOT_ROW_FOUND);
			
			Connection::singleton()->transaction_commit($link);
			return mysql_fetch_assoc($result);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($e);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function update($article_category_to) {
		try {
			$link = Connection::singleton()->transaction_begin();
			
			$title = mysql_real_escape_string($article_category_to->get_title());
			$description = mysql_real_escape_string($article_category_to->get_description());
			$ranking = $article_category_to->get_ranking();			
			
			$result = mysql_query("update article_category set 
									description='$description',	ranking='$ranking'", $link);
									
			if (!$result || (mysql_affected_rows($link) != 1))
				throw new Exception(NOT_ROW_UPDATED);
				
			Connection::singleton()->transaction_commit($link);
		}
		catch (Exception $e) {
			Connection::singleton()->transaction_rollback($e);
			Exception_handler::singleton()->handler($e);
		}
	}
	
	public function remove($number) {
		try {
			 $link = Connection::singleton()->transaction_begin();
			 
			 $result = mysql_query("delete from article_category
			 						where number='$number'", $link);
			 
			 if (!$result || (mysql_affected_rows($link) != 1))
			 	throw new Exception(NOT_ROW_DELETED);			
			
			Connection::singleton()->transaction_commit($link);
			
		}
		catch (Exception $e){
			Connection::singleton()->transaction_rollback($link);
			Exception_handler::singleton()->handler($e);
		}
	}
	
}
?>